// pages/balance/balance.js
import api from "../../api/index";
const { debounce } = require("../../utils/util");
const dayjs = require("dayjs");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    shopID: null,
    shopData: {
      name: "shop_id",
      list: [],
    },

    list: [],
    initialize: false,
    incomeInfo: null,
    date: null,
    explainShow: false,
    explainData: {
      title: "门店业绩月结",
      content:
        "总业绩：该月总业绩\n会员卡：该月会员卡业绩\n课程：该月课程业绩\n手续费：该月总支付手续费\n二次结算：会员该月跨店产生费用进行结算",
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      list: [],
      shopID: wx.getStorageSync("shopID"),
    });
    this.getShopList();
    this.getDataList();
  },

  getShopValue(e) {
    this.setData({
      list: [],
      shopID: e.detail.value,
    });
    this.getDataList();
  },
  getShopList() {
    api.apiStoreShopList().then((res) => {
      this.setData({
        "shopData.list": res.data.list,
      });
    });
  },
  getDataList() {
    return new Promise((resolve, reject) => {
      let { list, shopID, date } = this.data;
      let param = {
        shop_id: shopID,
      };
      if (date) param.date = date;
      api
        .apiStoreSett(param)
        .then((res) => {
          if (shopID !== this.data.shopID) return;
          let data = res.data;
          list = [...data.list];
          if (!date) {
            this.setData({
              hint: data.income_info.bottom_str,
            });
          }
          this.setData({
            incomeInfo: data.income_info,
            dateObj: {
              left: data.left_info.is_bright,
              leftDate: data.left_info.date,
              right: data.right_info.is_bright,
              rightDate: data.right_info.date,
            },
            date: data.title,
            list,
          });
        })
        .finally((_) => {
          this.setData({
            initialize: true,
          });
          resolve();
        });
    });
  },
  async switchDate(e) {
    let date = e.currentTarget.dataset.date;
    if (!date) {
      wx.showToast({
        title: "暂无数据",
        icon: "none",
      });
      return;
    }
    this.setData({
      date: dayjs(date).format("YYYY-MM"),
    });
    wx.showLoading({
      title: "加载中...",
      mask: true
    });
    await this.getDataList();
    wx.hideLoading();
  },
  explainClose() {
    this.setData({
      explainShow: false,
    });
  },
  explainOpen() {
    this.setData({
      explainShow: true,
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
